﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Carbon.Web.Forms.Controls;
using System.Web.Script.Serialization;

namespace Carbon.Web.Forms.jQueryForms.DataEditors.UploadEditor
{
    [Serializable]
    public class UploadEditorPopupViewModel
    {
        private FileReferenceCollection m_References = new FileReferenceCollection();
        public FileReferenceCollection References
        {
            get
            {
                return m_References;
            }
            set
            {
                m_References = value;
            }
        }
    }

    public class UploadEditorPopupDataContextControl : PopupDataContextControl<UploadEditorPopupViewModel>
    {
        protected override UploadEditorPopupViewModel CreateViewModel()
        {
            return new UploadEditorPopupViewModel();
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            this.DataBind();
        }
    }

    public partial class UploadEditorPopup : System.Web.UI.Page
    {
        public String AllowedExtensions
        {
            get
            {
                return this.Request.QueryString["AllowedExtensions"];
            }
        }

        public String MaxFiles
        {
            get
            {
                return this.Request.QueryString["MaxFiles"];
            }
        }

        protected UploadEditorPopupDataContextControl ctrl_Popup = null;
        public UploadEditorPopupDataContextControl Control_Popup
        {
          get
          {
             if (ctrl_Popup == null) throw new NullReferenceException();
            return ctrl_Popup;
          }
        }

        protected TextBox ctrl_Files = null;
        public TextBox Control_Files
        {
          get
          {
             if (ctrl_Files == null) throw new NullReferenceException();
            return ctrl_Files;
          }
        }
        
        protected override void OnLoad(EventArgs e)
        {
            this.Title = Resources.GetString("UploadFiles.Title");
            this.Page.ClientTarget = Constants.ClientAllPossibilities;

            if (this.IsPostBack)
            {
                JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
                String[] data = javascriptSerializer.Deserialize<String[]>(Control_Files.Text);

                 if ((data.Length % 3 == 0) == false)  throw new InvalidOperationException("(data.Length % 3 == 0) == false");
                
                for (int i = 0; i < data.Length; i += 3)
                {
                    Control_Popup.ViewModel.References.Add(new FileReference(data[i], new Guid(data[i + 1]), data[i + 2]));
                }

                Control_Popup.ClosePopup(true);
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            this.DataBind();
        }
    }
}